const toolModel = require('@/models/tool.model')
const Service = require('./index')
const { transformTree } = require('@/utils/common.util')
class ToolService extends Service {
  /**
   * 工具树形格式查询
   * @param {*} userId
   * @returns
   */
  async findToolTree (userId) {
    try {
      const classifys = await toolModel.findToolClassify()
      const tools = await toolModel.findTools(undefined, userId)
      const data = transformTree([...classifys, ...tools.map(o => {
        return {
          ...o,
          collected: Boolean(o.collected)
        }
      })], 'id', 'classifyId')
      const collectTool = await toolModel.findCollectTool(userId)
      return [{
        name: '我的收藏',
        id: 1000,
        icon: 'ri-heart-2-line',
        children: collectTool.map(o => {
          return {
            ...o,
            collected: true
          }
        })
      }, ...data]
    } catch (error) {
      throw new Error(error)
    }
  }

  async createToolClassify (attrs) {
    try {
      await toolModel.createToolClassify(attrs)
    } catch (error) {
      throw new Error(error)
    }
  }

  async findToolClassify () {
    try {
      const data = await toolModel.findToolClassify()
      return data
    } catch (error) {
      throw new Error(error)
    }
  }

  async createTool (attrs) {
    try {
      const classifyId = attrs.classifyId
      const existClassify = await toolModel.isExistClassify(classifyId)
      if (!existClassify) {
        return {
          errorMessage: '工具分类不存在'
        }
      }
      await toolModel.createTool(attrs)
    } catch (error) {
      throw new Error(error)
    }
  }

  async batchImportTools (classifyId, json) {
    try {
      const existClassify = await toolModel.isExistClassify(classifyId)
      if (!existClassify) {
        return {
          errorMessage: '工具分类不存在'
        }
      }
      const data = await Promise.allSettled(json.map(o => {
        return toolModel.createTool({
          ...o,
          classifyId
        })
      }))
      const successTotal = data.filter(o => o.status === 'fulfilled').length
      const failTotal = data.length - successTotal
      return {
        successTotal,
        failTotal
      }
    } catch (error) {
      throw new Error(error)
    }
  }

  async findTools (name) {
    try {
      const data = await toolModel.findTools({
        like: {
          'tool.name': name
        }
      })
      return data
    } catch (error) {
      throw new Error(error)
    }
  }

  async deleteToolClassifys (ids) {
    if (!ids?.length) {
      return {
        errorMessage: '必要标识ids不能为空'
      }
    }
    try {
      await toolModel.deleteToolClassifys(ids)
    } catch (error) {
      throw new Error(error)
    }
  }

  async deleteTools (ids) {
    if (!ids?.length) {
      return {
        errorMessage: '必要标识ids不能为空'
      }
    }
    try {
      await toolModel.deleteTools(ids)
    } catch (error) {
      throw new Error(error)
    }
  }

  async updateToolClassify (id, attrs) {
    if (this.isNull(id)) {
      return {
        errorMessage: '缺少必要标识id'
      }
    }
    try {
      const exist = await toolModel.isExistClassify(id)
      if (!exist) {
        return {
          errorMessage: '工具分类不存在'
        }
      }
      await toolModel.updateToolClassify(id, attrs)
    } catch (error) {
      throw new Error(error)
    }
  }

  async updateTool (id, attrs) {
    if (this.isNull(id)) {
      return {
        errorMessage: '缺少必要标识id'
      }
    }
    try {
      const exist = await toolModel.isExistTool(id)
      if (!exist) {
        return {
          errorMessage: '工具不存在'
        }
      }
      await toolModel.updateTool(id, attrs)
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new ToolService()
